#ifndef _ENTITYMANAGER_
#define _ENTITYMANAGER_

#include <list>
#include "InputStruct.hpp"

class Entity;
class MoveableEntity;


class EntityManager
{
public:
	EntityManager(){}
	
	void renderEntities();
	//renders all entities except the one specified by the parameter 	
	void renderWithoutEntity(Entity*);

	void renderCollisionModels();

	void onInput(InputStruct Input);
	void onTick(float time);
	
	void setEntities(std::list<Entity*>);

	/* deletes all objects pointed to by the members of list 'entities'.
	 * after that it removes all pointers from the list. */
	void clearEntities();

	void MoveEntity(MoveableEntity* theEntity, float Time);

	///return true, if the entity could be inserted
	bool addEntity(Entity*, bool CheckCollision=true);
	void markToRemove(Entity*);

	/* removes all entities from list 'entities' that were previously marked to 
	 * be removed next. (all members of list 'entitiesToRemove') */
	void removeEntities();	 
	
	//returns the first entity for that fun return true | bool fun(Entity*)
	template <typename FindFunction> Entity* FindEntity(FindFunction fun);

	//returns all entitys, that match the find function | bool fun(Entity*)
	template <typename FindFunction> std::list<Entity*> FindEntities(FindFunction fun);
private:
	std::list<Entity*> entities;

	std::list<Entity*> entitiesToRemove;	
};



//----- template implementation -----
template <typename FindFunction> Entity* EntityManager::FindEntity(FindFunction fun)
{
	for(std::list<Entity*>::iterator it=entities.begin(); it!=entities.end(); ++it)
	{
		if(fun(*it))
			return *it;
	}
	return NULL;
}


template <typename FindFunction> std::list<Entity*> EntityManager::FindEntities(FindFunction fun)
{
	std::list<Entity*> result;
	for(std::list<Entity*>::iterator it=entities.begin(); it!=entities.end(); ++it)
	{
		if(fun(*it))
			result.push_back(*it);
	}

	return result;
}



#endif // _ENTITYMANAGER_
